/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bl;

import Config.Localization;
import da.DAFineLoan;
import da.DALoanType;
import entities.FineLoan;
import entities.LoanType;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author foly
 */
public class BLFineLoan {

    DAFineLoan daFineLoan;

    public BLFineLoan() {
        daFineLoan = new DAFineLoan();
    }

    //get all loan type
    public ArrayList<FineLoan> getAll() throws Exception {
        return daFineLoan.getAll();
    }

    public ArrayList<FineLoan> getByID(String sid) throws Exception {
        int id = 0;
        boolean check = false;
        try {
            id = Integer.parseInt(sid);
            check = true;
        } catch (Exception ex) {
            check = false;
            //ex.printStackTrace();
        }
        if (check) {
            return daFineLoan.getByID(id);
        } else {
            JOptionPane.showMessageDialog(null, Localization.getMessage("SearchID"), "Error", JOptionPane.ERROR_MESSAGE);
            return daFineLoan.getAll();
        }
    }

    public boolean insert(String fromMoney, String toMoney, String fines) throws Exception {
        int result = 0;
        double to = 0;
        double from = 0;
        float fine = 0;
        boolean check = true;
        String error = "Error : \n"
                + "----------------------------------";

        //check from money
        if (fromMoney.equals("")) {
            check = false;
            error = error + "\n" + Localization.getMessage("FromNull");
        } else {
            try {
                from = Double.parseDouble(fromMoney);
                if(from <0){
                check = false;
                error = error + "\n" + "From Money can not less than 0.";

                }
            } catch (Exception ex) {
                check = false;
                error = error + "\n" + Localization.getMessage("FromError");
                //ex.printStackTrace();
            }
        }

        //check to money
        if (toMoney.equals("")) {
            check = false;
            error = error + "\n" + Localization.getMessage("ToNull");
        } else {
            try {
                to = Double.parseDouble(toMoney);
                if(to <0){
                check = false;
                error = error + "\n" + "To Money can not less than 0.";

                }
            } catch (Exception ex) {
                check = false;
                error = error + "\n" + Localization.getMessage("ToError");
                //ex.printStackTrace();
            }
        }

        if (fines.equals("")) {
            check = false;
            error = error + "\n" + Localization.getMessage("FineNull");
        } else {
            try {
                fine = Float.parseFloat(fines);
                if(fine <0){
                check = false;
                error = error + "\n" + "Fine can not less than 0.";

                }
            } catch (Exception ex) {
                check = false;
                error = error + "\n" + Localization.getMessage("FineError");
                //ex.printStackTrace();d
            }
        }

        if (check) {
            result = daFineLoan.insert(from, to, fine);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("InsertError"), "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public boolean update(String fineID, String fromMoney, String toMoney, String fines) throws Exception {
        int result = 0;
        int id = 0;
        double to = 0;
        double from = 0;
        float fine = 0;
        boolean check = true;


        String error = "Error : \n"
                + "----------------------------------";
        //check id
        try {
            id = Integer.parseInt(fineID);
        } catch (Exception e) {
            check = false;
            error = error + Localization.getMessage("SearchID");
            //e.printStackTrace();
        }

        //check from money
        if (fromMoney.equals("")) {
            check = false;
            error = error + "\n" + Localization.getMessage("FromNull");
        } else {
            try {
                from = Double.parseDouble(fromMoney);
            } catch (Exception ex) {
                check = false;
                error = error + "\n" + Localization.getMessage("FromError");
                //ex.printStackTrace();
            }
        }

        //check to money
        if (toMoney.equals("")) {
            check = false;
            error = error + "\n" + Localization.getMessage("ToNull");
        } else {
            try {
                to = Double.parseDouble(toMoney);
            } catch (Exception ex) {
                check = false;
                error = error + "\n" + Localization.getMessage("ToError");
                //ex.printStackTrace();
            }
        }

        if (fines.equals("")) {
            check = false;
            error = error + "\n" + Localization.getMessage("FineNull");
        } else {
            try {
                fine = Float.parseFloat(fines);
            } catch (Exception ex) {
                check = false;
                error = error + "\n" + Localization.getMessage("FineError");
                //ex.printStackTrace();
            }
        }


        if (check) {
            FineLoan fineLoan = new FineLoan(id, from, to, fine);
            result = daFineLoan.update(fineLoan);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getMessage("UpdateError"), "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public void delete(String fineID) throws Exception {
        int result = 0;
        int id = 0;
        boolean check = true;
        String error = "Error : \n"
                + "----------------------------------";
        try {
            id = Integer.parseInt(fineID);
        } catch (Exception ex) {
            check = false;
            error = error + "\n" + Localization.getMessage("SearchID");
            //ex.printStackTrace();
        }

        if (check) {
            result = daFineLoan.delete(id);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("delelteError"), "Error", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
